package org.nf.trading.controller.app.order.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.nf.trading.config.alipay.AliConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
public class PayController {
    @RequestMapping("/api/alipay/payApp")
    public String payment(String total,HttpServletResponse resp , HttpServletRequest req ,HttpSession session){
        String configAlipay = null;
        try {
            configAlipay = configAlipay(total,resp,req,session);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        return configAlipay;
    }
    public static String configAlipay(String sum, HttpServletResponse resp , HttpServletRequest req, HttpSession session) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(AliConfig.gatewayUrl);
        alipayConfig.setAppId(AliConfig.APP_ID);
        alipayConfig.setPrivateKey(AliConfig.APP_PRIVATE_KEY);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(AliConfig.ALIPAY_PUBLIC_KEY)  ;
        alipayConfig.setCharset(AliConfig.CHARSET);
        alipayConfig.setSignType(AliConfig.sign_type);
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);

        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        /*订单号*/
        model.setOutTradeNo(UUID.randomUUID().toString());
        /*订单总金额*///
        model.setTotalAmount(String.valueOf(sum));
        /*订单标题*/
        model.setSubject("ES");
        /*订单*/
        model.setProductCode("QUICK_WAP_WAY");
        /*设置完成后地址*/
        model.setPromoParams("");

        /*通过传过来的Id查询订单数据*/

        /*创建商品*/
        List<GoodsDetail> goodsDetail=new ArrayList<>();
        /*设置商品*/
        GoodsDetail goodsDetail1=new GoodsDetail();

        /*加入list*/
        goodsDetail.add(goodsDetail1);

        /*设置到model中*/
        model.setGoodsDetail(goodsDetail);

//        boolean rsaed = AlipaySignature.rsaCheckV1(new HashMap<>(), AliConfig.ALIPAY_PUBLIC_KEY, AliConfig.CHARSET);
//        if (rsaed){
//            System.out.println("未支付："+rsaed);
//        }

        request.setBizModel(model);
        request.setReturnUrl("http://localhost:8080/#/pages/order/completeOrder");
//        request.setNotifyUrl("http://localhost:8080/#/pages/my/my");
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);

        //输出
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
            System.out.println("response.getMerchantOrderNo() = " + response.getMerchantOrderNo());
            System.out.println("response.getTradeNo() = " + response.getTradeNo());
            System.out.println("response.getCode() = " + response.getCode());
        } else {
            System.out.println("调用失败");
        }
        return response.getBody();
    }
}
